// Srinivas Tamada http://9lessons.info
// wall.js

$(document)
		.ready(
				function() {
					// Update Status
					$(".update_button").click(function() {
						var updateval = $("#update").val();
						var dataString = 'update=' + updateval;
						if (updateval == '') {
							alert("Please Enter Some Text");
						} else {
							$("#flash").show();
							$("#flash").fadeIn(400).html('Loading Update...');
							$.ajax({
								type : "POST",
								url : "wall/post",
								data : dataString,
								cache : false,
								success : function(html) {
									$("#flash").fadeOut('slow');
									$("#content").prepend(html);
									$("#update").val('');
									$("#update").focus();
									$("#stexpand").oembed(updateval);
								}
							});
						}
						return false;
					});

					// commment Submit

					$('.comment_button').live(
							"click",
							function() {

								var ID = $(this).attr("id");

								var comment = $("#ctextarea" + ID).val();
								var dataString = 'comment=' + comment
										+ '&msg_id=' + ID;

								if (comment == '') {
									alert("Please Enter Comment Text");
								} else {
									$
											.ajax({
												type : "POST",
												url : "wall/comment",
												data : dataString,
												cache : false,
												success : function(html) {
													$("#commentload" + ID)
															.append(html);
													$("#ctextarea" + ID)
															.val('');
													$("#ctextarea" + ID)
															.focus();
												}
											});
								}
								return false;
							});
					// commentopen
					$('.commentopen').live("click", function() {
						var ID = $(this).attr("id");
						$("#commentbox" + ID).slideToggle('slow');
						return false;
					});

					// Load message
										
					$('.read_message').live("click", function() {
						var ID = $(this).attr("id");
						var dataString = 'msg_id=' + ID;
						alert(dataString);
						$.ajax({
							type : "POST",
							url : "messages/view",
							data : dataString,
							cache : false,
							success : function(html) {
								$("#read_comments" + ID).prepend(html);
								$("#streadmore" + ID).remove('');
								$("#twocomments" + ID).remove('');
							}
						});
						return false;
					});
					

						$('.loadmore').live("click", function() {
						$.ajax({
							type : "POST",
							url : "wall/loadmore",
							data : "",
							cache : false,
							success : function(html) {
								
								$("#content").append(retFind(html, 'DATA'));

								if (retFind(html, 'loadedcount') == 0) {
									$('#loadmorestatus').html("&nbsp;&nbsp;No More statuses");
								}
							}
						});
						return false;
					});

					// Likes
					$('.like')
							.live(
									"click",
									function() {
										var ID = $(this).attr("id");
										var like = $(this).attr("lk");
										var dataString = 'likes=' + like
												+ '&msg_id=' + ID;

										$
												.ajax({
													type : "POST",
													url : "wall/likes",
													data : dataString,
													cache : false,
													success : function(html) {
														$("#stlikes" + ID)
																.html(
																		retFind(
																				html,
																				'DATA'));

														if (like == 1) {
															$("#likes" + ID)
															.html("");
															$("#likes" + ID)
																	.html(
																			"<a href='#' class='like' lk = '0' id='"
																					+ ID
																					+ "'title='unlike'>&nbsp;&nbsp;unlike&nbsp;&nbsp;- &nbsp;&nbsp; </a>");
														} else {
															$("#likes" + ID)
																	.html(
																			"<a href='#' class='like' lk = '1' id='"
																					+ ID
																					+ "'title='like'>&nbsp;&nbsp;like&nbsp;&nbsp;- &nbsp;&nbsp; </a>");
														}

														if (retFind(html,
																'LIKECOUNT') == 0) {
															$(
																	"#stcommentloadlikes"
																			+ ID)
																	.remove('');
														}
													}
												});
										return false;
									});

					// delete comment
					$('.stcommentdelete')
							.live(
									"click",
									function() {
										var ID = $(this).attr("id");
										var dataString = 'com_id=' + ID;

										if (confirm("Sure you want to delete this update? There is NO undo!"
												+ ID)) {

											$.ajax({
												type : "POST",
												url : "wall/deletecomment",
												data : dataString,
												cache : false,
												success : function(html) {
													$("#stcommentbody" + ID)
															.slideUp();
												}
											});

										}
										return false;
									});
					// delete update
					$('.stdelete')
							.live(
									"click",
									function() {
										var ID = $(this).attr("id");
										var dataString = 'msg_id=' + ID;

										if (confirm("Sure you want to delete this update? There is NO undo!")) {

											$
													.ajax({
														type : "POST",
														url : "wall/deletestatus",
														data : dataString,
														cache : false,
														success : function(html) {
															$("#stbody" + ID)
																	.slideUp();
														}
													});
										}
										return false;
									});
				});

//////////////////////////////////////////////////////////
//
//For get string within search pattern
//
//////////////////////////////////////////////////////////
function retFind(str, searchPattern) {
	var findex;
	var lindex;
	var val;
	var s_tag;
	var e_tag;
	var tag_len;

	s_tag = "<" + searchPattern + ">";
	e_tag = "</" + searchPattern + ">";
	tag_len = s_tag.length;

	findex = str.indexOf(s_tag) + tag_len;
	lindex = str.indexOf(e_tag);

	val = str.substring(findex, lindex);
	return val;
}

/////////////////////////////////////////////////
//tool tip for the <a> tag
/////////////////////////////////////////////////
